#pragma once
#ifndef __CLIENTTABLE_H__
#define __CLIENTTABLE_H__

#include <unordered_map>
#include <util/Endpoint.hpp>


class ClientTable
{
public:
    ClientTable(uint32_t client_id):
        CLIENT_ID(client_id)
    { }

    void add_client(int fd, Endpoint endpoint){
        endpoint.app = CLIENT_ID;
        endpoint_to_fd.insert({endpoint, fd});
        fd_to_endpoint.insert({fd, endpoint});
    }

    void rm_client(int fd){
        Endpoint endpoint = fd_to_endpoint.at(fd);
        fd_to_endpoint.erase(fd);
        endpoint_to_fd.erase(endpoint);
    }

    bool contain(int fd){
        return fd_to_endpoint.count(fd);
    }

    bool contain(Endpoint endpoint){
        endpoint.app = CLIENT_ID;
        return endpoint_to_fd.count(endpoint);
    }

    Endpoint get(int fd){
        return fd_to_endpoint.at(fd);
    }

    int get(Endpoint endpoint){
        endpoint.app = CLIENT_ID;
        return endpoint_to_fd.at(endpoint);
    }

private:
    std::unordered_map<Endpoint, int> endpoint_to_fd;
    std::unordered_map<int, Endpoint> fd_to_endpoint;

    const uint32_t CLIENT_ID;
};

#endif // __CLIENTTABLE_H__